package com.atguigu.tingshu.album.service.impl;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.category.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<CategoryVo> getBaseCategoryList() {

        return baseCategoryViewMapper.getBaseCategoryList();
    }

	@Override
	public List<BaseAttribute> findAttribute(Long c1Id) {
		return baseAttributeMapper.findAttribute(c1Id);
	}

	@Override
	public List<BaseCategory3> findTopCategory3ByC1Id(Long c1Id) {
		//根据一级分类id,查询所有的2级分类集合
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, c1Id));
		if (CollectionUtils.isEmpty(baseCategory2s)){
			throw new GuiguException(201,"该一级分类下没有二级分类");
		}
		//获取2级分类id的集合
		List<Long> category2Ids = baseCategory2s.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());
		//3.根据二级分类id集合,查询所有的3级分类id,并且只留下置顶的7个
		List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, category2Ids)
				.eq(BaseCategory3::getIsTop, 1)
				.last("limit 7"));
		return baseCategory3s;

	}

    @Override
    public CategoryVo getBaseCategoryList(Long c1Id) {

		//创建一个返回对象类型的Vo
		CategoryVo categoryVo = new CategoryVo();
		//根据一级分类的id查询1级分类
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(c1Id);
		if (baseCategory1 == null){
			throw new GuiguException(201,"该一级分类不存在");
		}
		//根据一级分类id,查询所有的2级分类集合
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, c1Id));
		if (CollectionUtils.isEmpty(baseCategory2s)){
			throw new GuiguException(201,"该一级分类下没有二级分类");
		}
		ArrayList<CategoryVo> categoryVo1List = new ArrayList<>();
		for (BaseCategory2 baseCategory2 : baseCategory2s) {
			CategoryVo category2Vo = new CategoryVo();
			category2Vo.setCategoryId(baseCategory2.getId());//二级分类Id
			category2Vo.setCategoryName(baseCategory2.getName());//	二级分类名称
			List<BaseCategory3> category3s = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));
			if (CollectionUtils.isEmpty(category3s)){
				throw new GuiguException(201,"该二级分类下没有三级分类");
			}
			ArrayList<CategoryVo> categoryVo2List = new ArrayList<>();
			for (BaseCategory3 category3 : category3s) {
				CategoryVo category3Vo = new CategoryVo();
				category3Vo.setCategoryId(category3.getId());//三级分类id
				category3Vo.setCategoryName(category3.getName());//三级分类名称
				category3Vo.setCategoryChild(new ArrayList<>());
				categoryVo2List.add(category3Vo);
			}
			category2Vo.setCategoryChild(categoryVo2List);//二级分类的孩子
			categoryVo1List.add(category2Vo);
		}
		categoryVo.setCategoryId(c1Id);//一级分类id
		categoryVo.setCategoryName(baseCategory1.getName());//一级分类名称
		categoryVo.setCategoryChild(categoryVo1List);//一级分类的孩子
		return categoryVo;
    }

    @Override
    public List<BaseCategory1> getAllCategory1() {
		return baseCategory1Mapper.selectList(null);
    }
}
